<?
session_start();
include_once("secure/connect.php");
include_once("secure/functions.php");
if (!$_SESSION['logged'])
	{
	die ("SESSION FERMEE");
	}
if (!$_SESSION['rights']['compta']['insert'])
	{
	die ("no access");
	}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insérer une écriture comptable (toute sorte)</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
.liste {
	font-family: "Courier New", Courier, mono;
	font-size: small;
}
-->
</style>
<link href="../site_print.css" rel="stylesheet" type="text/css">
</head>

<body>

<?
$finaliser=0;

if (!$_GET['step'])
	{
	$_SESSION['cpt']=FALSE;
	echo '<a href="?step=14">&gt; Frais</a> - 95%<br>';
	echo '<a href="?step=3">&gt; Défraiment/avances sur BF</a> - 95%<br>';
	//echo '<a href="?step=4">&gt; Achat e-clipx (dépense)</a> - 95%<hr>';
	//echo '<a href="?step=5">&gt; Achat e-clipx (2ème étape, gestion des actifs) - 10%</a><br>';
	//echo '<a href="?step=5">&gt; Vente de matériel (gestion des actifs) - 10%</a>';
	}
	elseif ($_GET['step']==14) // frais - e-clipx
		{
		$_SESSION['cpt']['libelle']="frais";

		$_SESSION['cpt']['no_compte_debit']='35';
		$_SESSION['cpt']['no_compte_credit']='1';
		$sql_liste_debit="SELECT * FROM `cpt_comptes` WHERE ((no LIKE '35%' AND no NOT LIKE '359%') OR no LIKE '36%' OR no LIKE '37%' OR no LIKE '29%' OR no LIKE '23%' OR no LIKE '100%' OR no LIKE '101%') AND groupe !='1' AND active ='1' ORDER BY SUBSTRING(`no`,1),SUBSTRING(`no`,2),SUBSTRING(`no`,3),SUBSTRING(`no`,4),SUBSTRING(`no`,5),SUBSTRING(`no`,6),SUBSTRING(`no`,7),SUBSTRING(`no`,8),SUBSTRING(`no`,9) ";
		$sql_liste_credit="SELECT * FROM `cpt_comptes` WHERE (no LIKE '100%' OR no LIKE '101%' OR no LIKE '29%') AND groupe !='1' AND active ='1' ORDER BY SUBSTRING(`no`,1),SUBSTRING(`no`,2),SUBSTRING(`no`,3),SUBSTRING(`no`,4),SUBSTRING(`no`,5),SUBSTRING(`no`,6),SUBSTRING(`no`,7),SUBSTRING(`no`,8),SUBSTRING(`no`,9)";
		echo 'Frais - e-clipx<br>';
		//partie commune à 1x
		echo '
		<form action="" method="get" name="ecriture_comptable">
		<input type="hidden" name="step" value="100">
		Somme: <input type="text" name="somme" value=""> Fr<br>
		commentaire: <input type="text" name="commentaire"> / lié à fiche n° '.
		'<input type="text" name="id_fiche_loc_link" maxlength="50" size="20" />  
<input type="submit" name="fiche_loc" value="Trouver une fiche de location" class="liteoption" onClick="window.open('."'pop.recherche_fiche.php?mode=searchuser', '_phpbbsearch', 'HEIGHT=300,resizable=yes,WIDTH=600'".');return false;" />';
		//mk_list_table_class("id_fiche_loc_link","","SELECT fiches_loc.id, CONCAT(fiches_loc.id,' - ',contacts.prenom,' ',contacts.nom,'  \"',nom_event,'\" (',date_debut,')') as nom FROM fiches_loc, contacts WHERE contacts.id=fiches_loc.client ORDER BY id DESC LIMIT 0,100 ", "id", "nom", $db, TRUE, "champs");
		echo '<br> caisse: ';
		mk_list_table_class("compte_credit","",$sql_liste_credit, "no", "libelle_admin", $db, TRUE, "champs"); 
		echo '--&gt; charge: ';
		mk_list_table_class("compte_debit","",$sql_liste_debit, "no", "libelle_admin", $db, TRUE, "champs");
		echo '
		<br>
		date valeur: <input type="radio" name="date_valeur_now" value="1"> NOW <input type="radio" name="date_valeur_now" value="2" > demain <input type="radio" name="date_valeur_now" value="3"> <input type="text" name="date_valeur" value="'.date("Y-m-d H:i:s").'">(date effective de la transaction, sur le CCP par exemple)<br>
		
		date transaction: <input type="radio" name="date_transaction_now" value="4" checked="checked"> comme valeur <input type="radio" name="date_transaction_now" value="1"> NOW <input type="radio" name="date_transaction_now" value="2" > demain <input type="radio" name="date_transaction_now" value="3"> <input type="text" name="date_transaction" value="'.date("Y-m-d H:i:s").'">(date de la transaction, au magasin par exemple pour les cartes)<br>
		
		date facture: <input type="radio" name="date_facture_now" value="4" checked="checked"> comme valeur <input type="radio" name="date_facture_now" value="1"> NOW <input type="radio" name="date_facture_now" value="2" > demain <input type="radio" name="date_facture_now" value="3"> <input type="text" name="date_facture" value="'.date("Y-m-d").'">(date d\'émission de la facture par le fournisseur)<br>
		<input type="submit" value="OK"> 
		</form>
		';
		}
	elseif ($_GET['step']==100) // récap groupe 1
		{
		echo 'vérifier les données !!';
		if ($_GET['date_valeur_now']=='1')
			{
			$_SESSION['cpt']['date_valeur']=date("Y-m-d H:i:s");
				}
		elseif ($_GET['date_valeur_now']=='2')
			{
			$_SESSION['cpt']['date_valeur']=date("Y-m-d 00:00:01",(time()+24*3600));
			}
		elseif ($_GET['date_valeur_now']=='3')
			{
			$_SESSION['cpt']['date_valeur']=$_GET['date_valeur'];
			}
			
		if ($_GET['date_transaction_now']=='1')
			{
			$_SESSION['cpt']['date_transaction']=date("Y-m-d H:i:s");
			}
		elseif ($_GET['date_transaction_now']=='2')
			{
			$_SESSION['cpt']['date_transaction']=date("Y-m-d 00:00:01",(time()+24*3600));
			}
		elseif ($_GET['date_transaction_now']=='3')
			{
			$_SESSION['cpt']['date_transaction']=$_GET['date_transaction'];
			}
		elseif ($_GET['date_transaction_now']=='4')
			{
			$_SESSION['cpt']['date_transaction']=$_SESSION['cpt']['date_valeur'];
			}
			
			
		if ($_GET['date_facture_now']=='1')
			{
			$_SESSION['cpt']['date_facture']=date("Y-m-d");
			}
		elseif ($_GET['date_facture_now']=='2')
			{
			$_SESSION['cpt']['date_facture']=date("Y-m-d",(time()+24*3600));
			}
		elseif ($_GET['date_facture_now']=='3')
			{
			$_SESSION['cpt']['date_facture']=$_GET['date_facture'];
			}
		elseif ($_GET['date_facture_now']=='4')
			{
			$_SESSION['cpt']['date_facture']=$_SESSION['cpt']['date_valeur'];
			}
			
			
		 switch (substr($_GET['compte_debit'],0,1))
		 	{
			case 1: $_SESSION['cpt']['libelle']="transfert"; break;
			case 2: if (substr($_GET['compte_debit'],1,1)==3) $_SESSION['cpt']['libelle']="remboursement prêt"; else $_SESSION['cpt']['libelle']="paiement passif"; break;
			case 3: $_SESSION['cpt']['libelle']="frais"; break;
			}
		 $_SESSION['cpt']['somme']=$_GET['somme'];
		 $_SESSION['cpt']['no_compte_credit']=$_GET['compte_credit'];
		 $_SESSION['cpt']['no_compte_debit']=$_GET['compte_debit'];
		 $_SESSION['cpt']['commentaire']=$_GET['commentaire'];
		 $_SESSION['cpt']['id_fiche_loc_link']=$_GET['id_fiche_loc_link'];
		}

	elseif ($_GET['step']==20) 
		{
		echo 'vérifier les données !!';
		if ($_GET['date_valeur_now']=='1')
			{
			$_SESSION['cpt']['date_valeur']=date("Y-m-d H:i:s");
			}
		elseif ($_GET['date_valeur_now']=='2')
			{
			$_SESSION['cpt']['date_valeur']=date("Y-m-d 00:00:01",(time()+24*3600));
			}
		elseif ($_GET['date_valeur_now']=='3')
			{
			$_SESSION['cpt']['date_valeur']=$_GET['date_valeur'];
			}
			
		if ($_GET['date_transaction_now']=='1')
			{
			$_SESSION['cpt']['date_transaction']=date("Y-m-d H:i:s");
			}
		elseif ($_GET['date_transaction_now']=='2')
			{
			$_SESSION['cpt']['date_transaction']=date("Y-m-d 00:00:01",(time()+24*3600));
			}
		elseif ($_GET['date_transaction_now']=='3')
			{
			$_SESSION['cpt']['date_transaction']=$_GET['date_transaction'];
			}
		elseif ($_GET['date_transaction_now']=='4')
			{
			$_SESSION['cpt']['date_transaction']=$_SESSION['cpt']['date_valeur'];
			}
			
			
		if ($_GET['date_facture_now']=='1')
			{
			$_SESSION['cpt']['date_facture']=date("Y-m-d");
			}
		elseif ($_GET['date_facture_now']=='2')
			{
			$_SESSION['cpt']['date_facture']=date("Y-m-d",(time()+24*3600));
			}
		elseif ($_GET['date_facture_now']=='3')
			{
			$_SESSION['cpt']['date_facture']=$_GET['date_facture'];
			}
		elseif ($_GET['date_facture_now']=='4')
			{
			$_SESSION['cpt']['date_facture']=$_SESSION['cpt']['date_valeur'];
			}
			
		$_SESSION['cpt']['commentaire']=FALSE;
		if ($_GET['compte_debit']==3460)
			{
			$_SESSION['cpt']['commentaire']="\"".$_GET['nom_staff']."\" ";
			}
		if ($_GET['type_salaire']=="mensuel")
		 	{
			if ($_GET['mois']<10) {$_GET['mois']='0'.$_GET['mois'];}
			$_SESSION['cpt']['commentaire']=$_SESSION['cpt']['commentaire'].$_GET['annee'].'-'.$_GET['mois'];
			}
		elseif ($_GET['type_salaire']=="extraordinaire")
			{
			$_SESSION['cpt']['commentaire']=$_SESSION['cpt']['commentaire'].$_GET['commentaire'];
			if ($_GET['id_fiche_loc_link'])
				{
				$_SESSION['cpt']['commentaire']=$_SESSION['cpt']['commentaire'].' (fiche '.$_GET['id_fiche_loc_link'].')';
				}
			}
		 $_SESSION['cpt']['somme']=$_GET['somme'];
		 $_SESSION['cpt']['no_compte_credit']=$_GET['compte_credit'];
		 $_SESSION['cpt']['no_compte_debit']=$_GET['compte_debit'];
		 $_SESSION['cpt']['id_fiche_loc_link']=$_GET['id_fiche_loc_link'];
		}

elseif ($_GET['step']==3) // Défraiment e-clipx
	{
	$_SESSION['cpt']['libelle']="paiment";
	$_SESSION['cpt']['no_compte_debit']='359';
	$_SESSION['cpt']['no_compte_credit']='1';
	echo 'Défraiment e-clipx<br>';
	echo '
	<form action="" method="get">
	<input type="hidden" name="step" value="30">
	Somme: <input type="text" name="somme" value=""> Fr<br>
	<input type="text" name="commentaire"> lié à fiche n° '.'<input type="text" name="id_fiche_loc_link" maxlength="50" size="20" />  
<input type="submit" name="fiche_loc" value="Trouver une fiche de location" class="liteoption" onClick="window.open('."'pop.recherche_fiche.php?mode=searchuser', '_phpbbsearch', 'HEIGHT=300,resizable=yes,WIDTH=600'".');return false;" />';

	echo '<br>
	Comptes salaires:';
	mk_list_table_class("compte_debit","","SELECT * FROM `cpt_comptes` WHERE (no LIKE '359%' AND no NOT IN (35901,35902,35903,35904) OR no LIKE '113%') AND groupe !='1' AND active ='1' ORDER BY SUBSTRING(`no`,1),SUBSTRING(`no`,2),SUBSTRING(`no`,3),SUBSTRING(`no`,4),SUBSTRING(`no`,5),SUBSTRING(`no`,6),SUBSTRING(`no`,7),SUBSTRING(`no`,8),SUBSTRING(`no`,9) ", "no", "libelle_admin", $db, TRUE, "champs");
	echo ' si STAFF, nom prénom: <input type="text" name="nom_staff" value=""><br>
	Caisses: ';
	mk_list_table_class("compte_credit","","SELECT * FROM `cpt_comptes` WHERE (no LIKE '100%' OR no LIKE '101%' OR no LIKE '29%') AND groupe !='1' AND active ='1' ORDER BY SUBSTRING(`no`,1),SUBSTRING(`no`,2),SUBSTRING(`no`,3),SUBSTRING(`no`,4),SUBSTRING(`no`,5),SUBSTRING(`no`,6),SUBSTRING(`no`,7),SUBSTRING(`no`,8),SUBSTRING(`no`,9) ", "no", "libelle_admin", $db, TRUE, "champs"); 
	echo '
	<br>
		date valeur: <input type="radio" name="date_valeur_now" value="1"> NOW <input type="radio" name="date_valeur_now" value="2" > demain <input type="radio" name="date_valeur_now" value="3"> <input type="text" name="date_valeur" value="'.date("Y-m-d H:i:s").'">(date effective de la transaction, sur le CCP par exemple)<br>
		
		date transaction: <input type="radio" name="date_transaction_now" value="4" checked="checked"> comme valeur <input type="radio" name="date_transaction_now" value="1"> NOW <input type="radio" name="date_transaction_now" value="2" > demain <input type="radio" name="date_transaction_now" value="3"> <input type="text" name="date_transaction" value="'.date("Y-m-d H:i:s").'">(date de la transaction, au magasin par exemple pour les cartes)<br>
		
		date facture: <input type="radio" name="date_facture_now" value="4" checked="checked"> comme valeur <input type="radio" name="date_facture_now" value="1"> NOW <input type="radio" name="date_facture_now" value="2" > demain <input type="radio" name="date_facture_now" value="3"> <input type="text" name="date_facture" value="'.date("Y-m-d").'">(date d\'émission de la facture par le fournisseur)<br>
	
	<input type="submit" value="OK">
	</form>
	';
	}
	elseif ($_GET['step']==30) 
		{
		echo 'vérifier les données !!';
		if ($_GET['date_valeur_now']=='1')
			{
			$_SESSION['cpt']['date_valeur']=date("Y-m-d H:i:s");
			}
		elseif ($_GET['date_valeur_now']=='2')
			{
			$_SESSION['cpt']['date_valeur']=date("Y-m-d 00:00:01",(time()+24*3600));
			}
		elseif ($_GET['date_valeur_now']=='3')
			{
			$_SESSION['cpt']['date_valeur']=$_GET['date_valeur'];
			}
			
		if ($_GET['date_transaction_now']=='1')
			{
			$_SESSION['cpt']['date_transaction']=date("Y-m-d H:i:s");
			}
		elseif ($_GET['date_transaction_now']=='2')
			{
			$_SESSION['cpt']['date_transaction']=date("Y-m-d 00:00:01",(time()+24*3600));
			}
		elseif ($_GET['date_transaction_now']=='3')
			{
			$_SESSION['cpt']['date_transaction']=$_GET['date_transaction'];
			}
		elseif ($_GET['date_transaction_now']=='4')
			{
			$_SESSION['cpt']['date_transaction']=$_SESSION['cpt']['date_valeur'];
			}
			
			
		if ($_GET['date_facture_now']=='1')
			{
			$_SESSION['cpt']['date_facture']=date("Y-m-d");
			}
		elseif ($_GET['date_facture_now']=='2')
			{
			$_SESSION['cpt']['date_facture']=date("Y-m-d",(time()+24*3600));
			}
		elseif ($_GET['date_facture_now']=='3')
			{
			$_SESSION['cpt']['date_facture']=$_GET['date_facture'];
			}
		elseif ($_GET['date_facture_now']=='4')
			{
			$_SESSION['cpt']['date_facture']=$_SESSION['cpt']['date_valeur'];
			}
			
		$_SESSION['cpt']['commentaire']=FALSE;
		if ($_GET['compte_debit']==3505)
			{
			$_SESSION['cpt']['commentaire']="\"".$_GET['nom_staff']."\" ";
			}
		$_SESSION['cpt']['commentaire']=$_SESSION['cpt']['commentaire'].$_GET['commentaire'];
		if ($_GET['id_fiche_loc_link'])
			{
			$_SESSION['cpt']['commentaire']=$_SESSION['cpt']['commentaire'].' (fiche '.$_GET['id_fiche_loc_link'].')';
			}
		 $_SESSION['cpt']['somme']=$_GET['somme'];
		 $_SESSION['cpt']['no_compte_credit']=$_GET['compte_credit'];
		 $_SESSION['cpt']['no_compte_debit']=$_GET['compte_debit'];
		 $_SESSION['cpt']['id_fiche_loc_link']=$_GET['id_fiche_loc_link'];
		}

elseif ($_GET['step']==4) // Achat e-clipx
	{
	$_SESSION['cpt']['libelle']="achat";
	$_SESSION['cpt']['no_compte_debit']='3513';
	$_SESSION['cpt']['no_compte_credit']='1';
	echo 'achat<br>';
	echo '
	<form action="" method="get">
	<input type="hidden" name="step" value="40">
	Description:<input type="text" name="commentaire"><br>
	Somme: <input type="text" name="somme" value=""> Fr<br>';

	mk_list_table_class("compte_credit","","SELECT * FROM `cpt_comptes` WHERE (no LIKE '100%' OR no LIKE '101%') AND groupe !='1' AND active ='1' ORDER BY SUBSTRING(`no`,1),SUBSTRING(`no`,2),SUBSTRING(`no`,3),SUBSTRING(`no`,4),SUBSTRING(`no`,5),SUBSTRING(`no`,6),SUBSTRING(`no`,7),SUBSTRING(`no`,8),SUBSTRING(`no`,9) ", "no", "libelle_admin", $db, TRUE, "champs");
	echo '
	<br>
	date: <input type="radio" name="date_valeur_now" value="1"> NOW <input type="radio" name="date_valeur_now" value="2" > demain <input type="radio" name="date_valeur_now" value="3"> <input type="text" name="date_valeur" value="'.date("Y-m-d H:i:s").'"><br>
	<input type="submit" value="OK">
	</form>
	';
	}
	elseif ($_GET['step']==40) 
		{
		echo 'vérifier les données !!';
		if ($_GET['date_valeur_now']=='1')
			{
			$_SESSION['cpt']['date_valeur']=date("Y-m-d H:i:s");
			}
		elseif ($_GET['date_valeur_now']=='2')
			{
			$_SESSION['cpt']['date_valeur']=date("Y-m-d 00:00:01",(time()+24*3600));
			}
		elseif ($_GET['date_valeur_now']=='3')
			{
			$_SESSION['cpt']['date_valeur']=$_GET['date_valeur'];
			}
			
		if ($_GET['date_transaction_now']=='1')
			{
			$_SESSION['cpt']['date_transaction']=date("Y-m-d H:i:s");
			}
		elseif ($_GET['date_transaction_now']=='2')
			{
			$_SESSION['cpt']['date_transaction']=date("Y-m-d 00:00:01",(time()+24*3600));
			}
		elseif ($_GET['date_transaction_now']=='3')
			{
			$_SESSION['cpt']['date_transaction']=$_GET['date_transaction'];
			}
		elseif ($_GET['date_transaction_now']=='4')
			{
			$_SESSION['cpt']['date_transaction']=$_SESSION['cpt']['date_valeur'];
			}
			
			
		if ($_GET['date_facture_now']=='1')
			{
			$_SESSION['cpt']['date_facture']=date("Y-m-d");
			}
		elseif ($_GET['date_facture_now']=='2')
			{
			$_SESSION['cpt']['date_facture']=date("Y-m-d",(time()+24*3600));
			}
		elseif ($_GET['date_facture_now']=='3')
			{
			$_SESSION['cpt']['date_facture']=$_GET['date_facture'];
			}
		elseif ($_GET['date_facture_now']=='4')
			{
			$_SESSION['cpt']['date_facture']=$_SESSION['cpt']['date_valeur'];
			}
			
		$_SESSION['cpt']['commentaire']=$_GET['commentaire'];
		$_SESSION['cpt']['somme']=$_GET['somme'];
		$_SESSION['cpt']['no_compte_credit']=$_GET['compte_credit'];
		$_SESSION['cpt']['id_fiche_loc_link']=$_GET['id_fiche_loc_link'];
		}

elseif ($_GET['step']==5) // Achat e-clipx (2ème étape)
	{
	$_SESSION['cpt']['libelle']="achat";
	$_SESSION['cpt']['no_compte_debit']='111';
	$_SESSION['cpt']['no_compte_credit']='3513';
	echo 'achat<br>';
	mk_list_table_class("compte_debit","1011","SELECT cpt_comptes.no as no, CONCAT(inventaire_description.nom_court,' (id: ',inventaire.id,')') as nom FROM `cpt_comptes`, inventaire, inventaire_description WHERE cpt_comptes.id_inventaire=inventaire.id AND inventaire.id_inventaire_description=inventaire_description.id AND (cpt_comptes.no LIKE '111%') AND cpt_comptes.groupe !='1' AND cpt_comptes.active ='1' ORDER BY cpt_comptes.no DESC ", "no", "nom", $db, FALSE, "champs");

	}
elseif ($_GET['step']==99) // insertion SQL
	{
	if ($_SESSION['rand_secu']==$_GET['rand_secu'])
		{
		$sql="INSERT INTO cpt_journal 
		(`date` , `date_transaction` ,`date_facture` ,`date_ecriture` , `no_compte_debit` , `no_compte_credit` , 
		`somme` , `libelle` , `commentaire` , 
		`id_client` , `id_fiche_loc` , `id_inventaire` , 
		`id_admin`  , `id_fiche_loc_link` ) 
		VALUES 
		('".$_SESSION['cpt']['date_valeur']."','".$_SESSION['cpt']['date_transaction']."','".$_SESSION['cpt']['date_facture']."',NOW(),'".$_SESSION['cpt']['no_compte_debit']."','".$_SESSION['cpt']['no_compte_credit']."',
		'".$_SESSION['cpt']['somme']."','".$_SESSION['cpt']['libelle']."','".$_SESSION['cpt']['commentaire']."',
		'".$_SESSION['cpt']['id_client']."','".$_SESSION['cpt']['id_fiche_loc']."','".$_SESSION['cpt']['id_inventaire']."',
		'".$_SESSION['user_id']."','".$_SESSION['cpt']['id_fiche_loc_link']."')";
		//echo $sql;
		mysql_query($sql,$db);
		//print_r($_SESSION);
		$_SESSION['rand_secu']=0;
		//Récupération du dernier ID
		$sql="SELECT max(id) as id FROM cpt_journal";
		$res=mysql_query($sql,$db);
		$ligne=mysql_fetch_assoc($res);
		echo "Dernier ID inséré:".$ligne['id'];
		}
	else
		{
		echo "c'est mal d'appuyer sur F5!!";
		}
	}
?>
<hr>
<?
$sql="SELECT libelle_admin, groupe FROM cpt_comptes WHERE no='".$_SESSION['cpt']['no_compte_debit']."'";
$res=mysql_query($sql,$db);
$ligne=mysql_fetch_assoc($res);
$_SESSION['cpt']['libelle_compte_debit']=$ligne['libelle_admin'];
if ($ligne['groupe']) 
	{
	$_SESSION['cpt']['libelle_compte_debit']='<i>groupe </i>'.$_SESSION['cpt']['libelle_compte_debit'];
	$_SESSION['cpt']['groupe_compte_debit']=1;
	}
else
	{
	$_SESSION['cpt']['groupe_compte_debit']=0;
	}

$sql="SELECT libelle_admin, groupe FROM cpt_comptes WHERE no='".$_SESSION['cpt']['no_compte_credit']."'";
$res=mysql_query($sql,$db);
$ligne=mysql_fetch_assoc($res);
$_SESSION['cpt']['libelle_compte_credit']=$ligne['libelle_admin'];
if ($ligne['groupe']) 
	{
	$_SESSION['cpt']['libelle_compte_credit']='<i>groupe </i>'.$_SESSION['cpt']['libelle_compte_credit'];
	$_SESSION['cpt']['groupe_compte_credit']=1;
	}
else
	{
	$_SESSION['cpt']['groupe_compte_credit']=0;
	}
// vérifie si les compte est vérouillé pour la date d'insertion
$date_insertion=format_datetime2($_SESSION['cpt']['date_valeur'],"U");
$date_now=time();
$sql="SELECT * FROM cpt_verrou WHERE no_cpt_comptes='".$_SESSION['cpt']['no_compte_credit']."'";
$res=mysql_query($sql,$db);
$ligne=mysql_fetch_assoc($res);
if ($ligne['type']=="total")
	{
	$_SESSION['cpt']['valide']['verrou_compte_credit']=0;
	$_SESSION['cpt']['valide_comment']['verrou_compte_credit']='compte vérouillé';
	}
elseif ($ligne['type']=="rel")
	{
	if ($date_insertion-$date_now <= $ligne['a_relatif'] OR $date_insertion-$date_now <= $ligne['de_relatif'])
		{
		$_SESSION['cpt']['valide']['verrou_compte_credit']=0;
		$_SESSION['cpt']['valide_comment']['verrou_compte_credit']='compte vérouillé sur ces dates relatives';
		}
	else
		{
		$_SESSION['cpt']['valide']['verrou_compte_credit']=1;
		$_SESSION['cpt']['valide_comment']['verrou_compte_credit']='éciture permise';
		}
	}
elseif ($ligne['type']=="abs")
	{
	if ($_SESSION['cpt']['date_valeur']>$ligne['de_absolu'] AND $_SESSION['cpt']['date_valeur']<$ligne['a_absolu'])
		{
		$_SESSION['cpt']['valide']['verrou_compte_credit']=0;
		$_SESSION['cpt']['valide_comment']['verrou_compte_credit']='compte vérouillé sur ces dates';
		}
	else
		{
		$_SESSION['cpt']['valide']['verrou_compte_credit']=1;
		$_SESSION['cpt']['valide_comment']['verrou_compte_credit']='éciture permise';
		}
	}
else
	{
	$_SESSION['cpt']['valide']['verrou_compte_credit']=1;
	$_SESSION['cpt']['valide_comment']['verrou_compte_credit']='éciture permise';
	}

// vérifie si les compte est vérouillé pour la date d'insertion DEBIT
$sql="SELECT * FROM cpt_verrou WHERE no_cpt_comptes='".$_SESSION['cpt']['no_compte_debit']."'";
$res=mysql_query($sql,$db);
$ligne=mysql_fetch_assoc($res);
if ($ligne['type']=="total")
	{
	$_SESSION['cpt']['valide']['verrou_compte_debit']=0;
	$_SESSION['cpt']['valide_comment']['verrou_compte_debit']='compte vérouillé';
	}
elseif ($ligne['type']=="rel")
	{
	if ($date_insertion-$date_now <= $ligne['a_relatif'] OR $date_insertion-$date_now <= $ligne['de_relatif'])
		{
		$_SESSION['cpt']['valide']['verrou_compte_debit']=0;
		$_SESSION['cpt']['valide_comment']['verrou_compte_debit']='compte vérouillé sur ces dates relatives';
		}
	else
		{
		$_SESSION['cpt']['valide']['verrou_compte_debit']=1;
		$_SESSION['cpt']['valide_comment']['verrou_compte_debit']='éciture permise';
		}
	}
elseif ($ligne['type']=="abs")
	{
	if ($_SESSION['cpt']['date_valeur']>$ligne['de_absolu'] AND $_SESSION['cpt']['date_valeur']<$ligne['a_absolu'])
		{
		$_SESSION['cpt']['valide']['verrou_compte_debit']=0;
		$_SESSION['cpt']['valide_comment']['verrou_compte_debit']='compte vérouillé sur ces dates';
		}
	else
		{
		$_SESSION['cpt']['valide']['verrou_compte_debit']=1;
		$_SESSION['cpt']['valide_comment']['verrou_compte_debit']='éciture permise';
		}
	}
else
	{
	$_SESSION['cpt']['valide']['verrou_compte_debit']=1;
	$_SESSION['cpt']['valide_comment']['verrou_compte_debit']='éciture permise';
	}

function affiche_petit_point ($etat,$commentaire)
	{
	if ($etat==1) {$lettre='g';}
	elseif ($etat==0.5) {$lettre='o';}
	elseif ($etat==0) {$lettre='r';}
	return '<img src="../images/act'.$lettre.'_15.gif" Title="'.$commentaire.'">';
	}
	
if ($_SESSION['cpt']['date_valeur'])//a faire, vérifier si la date est valide
	{
	$_SESSION['cpt']['valide']['date_valeur']=1; 
	$_SESSION['cpt']['valide_comment']['date_valeur']='OK';
	}
else
	{
	$_SESSION['cpt']['valide']['date_valeur']=0; 
	$_SESSION['cpt']['valide_comment']['date_valeur']='date invalide';
	}
	
if ($_SESSION['cpt']['date_transaction'])//a faire, vérifier si la date est valide
	{
	$_SESSION['cpt']['valide']['date_transaction']=1; 
	$_SESSION['cpt']['valide_comment']['date_transaction']='OK';
	}
else
	{
	$_SESSION['cpt']['valide']['date_transaction']=0; 
	$_SESSION['cpt']['valide_comment']['date_transaction']='date invalide';
	}
	
if ($_SESSION['cpt']['date_facture'])//a faire, vérifier si la date est valide
	{
	$_SESSION['cpt']['valide']['date_facture']=1; 
	$_SESSION['cpt']['valide_comment']['date_facture']='OK';
	}
else
	{
	$_SESSION['cpt']['valide']['date_facture']=0; 
	$_SESSION['cpt']['valide_comment']['date_facture']='date invalide';
	}

if ($_SESSION['cpt']['libelle']) // à améliorer, vérifier si membre de la liste de libellés std
	{
	$_SESSION['cpt']['valide']['libelle']=1;
	$_SESSION['cpt']['valide_comment']['libelle']='OK';
	}
else
	{
	$_SESSION['cpt']['valide']['libelle']=0;
	$_SESSION['cpt']['valide_comment']['libelle']='manque libellé';
	}

if ($_SESSION['cpt']['commentaire'])
	{
	$_SESSION['cpt']['valide']['commentaire']=1;
	$_SESSION['cpt']['valide_comment']['commentaire']='OK';
	}
else
	{
	$_SESSION['cpt']['valide']['commentaire']=0.5;
	$_SESSION['cpt']['valide_comment']['commentaire']='pas indispensable';
	}

if ($_SESSION['cpt']['somme']>0 && is_numeric($_SESSION['cpt']['somme']))
	{
	$_SESSION['cpt']['valide']['somme']=1;
	$_SESSION['cpt']['valide_comment']['somme']='OK';
	}
else
	{
	$_SESSION['cpt']['valide']['somme']=0;
	$_SESSION['cpt']['valide_comment']['somme']='somme invalide';
	}


if ($_SESSION['cpt']['no_compte_debit'] && $_SESSION['cpt']['libelle_compte_debit'] && !$_SESSION['cpt']['groupe_compte_debit'])
	{
	$_SESSION['cpt']['valide']['no_compte_debit']=1;
	$_SESSION['cpt']['valide_comment']['no_compte_debit']='OK';
	}
elseif ($_SESSION['cpt']['no_compte_debit'] && $_SESSION['cpt']['libelle_compte_debit'] && $_SESSION['cpt']['groupe_compte_debit'])
	{
	$_SESSION['cpt']['valide']['no_compte_debit']=0;
	$_SESSION['cpt']['valide_comment']['no_compte_debit']='GROUPE, pas compte';
	}
else
	{
	$_SESSION['cpt']['valide']['no_compte_debit']=0;
	$_SESSION['cpt']['valide_comment']['no_compte_debit']='compte invalide';
	}

if ($_SESSION['cpt']['no_compte_credit'] && $_SESSION['cpt']['libelle_compte_credit'] && !$_SESSION['cpt']['groupe_compte_credit'])
	{
	$_SESSION['cpt']['valide']['no_compte_credit']=1;
	$_SESSION['cpt']['valide_comment']['no_compte_credit']='OK';
	}
elseif ($_SESSION['cpt']['no_compte_debit'] && $_SESSION['cpt']['libelle_compte_credit'] && $_SESSION['cpt']['groupe_compte_credit'])
	{
	$_SESSION['cpt']['valide']['no_compte_credit']=0;
	$_SESSION['cpt']['valide_comment']['no_compte_credit']='GROUPE, pas compte';
	}
else
	{
	$_SESSION['cpt']['valide']['no_compte_credit']=0;
	$_SESSION['cpt']['valide_comment']['no_compte_credit']='compte invalide';
	}



$_SESSION['cpt']['valide']['id_inventaire']=0.5;
$_SESSION['cpt']['valide_comment']['id_inventaire']='OK';

$_SESSION['cpt']['valide']['id_client']=0.5;
$_SESSION['cpt']['valide_comment']['id_client']='OK';

$_SESSION['cpt']['valide']['id_fiche_loc']=0.5;
$_SESSION['cpt']['valide_comment']['id_fiche_loc']='OK';

if ($_SESSION['cpt']['id_fiche_loc_link']>0 && is_numeric($_SESSION['cpt']['id_fiche_loc_link']))
	{
	$_SESSION['cpt']['valide']['id_fiche_loc_link']=1;
	$_SESSION['cpt']['valide_comment']['id_fiche_loc_link']='OK';

	}
else
	{
	$_SESSION['cpt']['valide']['id_fiche_loc_link']=0.5;
	$_SESSION['cpt']['valide_comment']['id_fiche_loc_link']='OK';
	}

?>
<table border="1">
  <tr>
    <td>date valeur:</td>
    <td><? echo $_SESSION['cpt']['date_valeur']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['date_valeur'],$_SESSION['cpt']['valide_comment']['date_valeur']); ?></td>
    <td>ID inventaire </td>
    <td><? echo $_SESSION['cpt']['id_inventaire']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['id_inventaire'],$_SESSION['cpt']['valide_comment']['id_inventaire']); ?></td>
  </tr>
  <tr>
    <td>date transac:</td>
    <td><? echo $_SESSION['cpt']['date_transaction']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['date_transaction'],$_SESSION['cpt']['valide_comment']['date_transaction']); ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>date facture:</td>
    <td><? echo $_SESSION['cpt']['date_facture']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['date_facture'],$_SESSION['cpt']['valide_comment']['date_facture']); ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>libelle</td>
    <td><? echo $_SESSION['cpt']['libelle']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['libelle'],$_SESSION['cpt']['valide_comment']['libelle']); ?></td>
    <td>ID client</td>
    <td><? echo $_SESSION['cpt']['id_client']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['id_client'],$_SESSION['cpt']['valide_comment']['id_client']); ?></td>
  </tr>
  <tr>
    <td>commentaire</td>
    <td><? echo $_SESSION['cpt']['commentaire']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['commentaire'],$_SESSION['cpt']['valide_comment']['commentaire']); ?></td>
    <td>ID fiche loc</td>
    <td><? echo $_SESSION['cpt']['id_fiche_loc']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['id_fiche_loc'],$_SESSION['cpt']['valide_comment']['id_fiche_loc']); ?></td>
  </tr>
  <tr>
    <td>somme</td>
    <td><? echo $_SESSION['cpt']['somme']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['somme'],$_SESSION['cpt']['valide_comment']['somme']); ?></td>
    <td>ID fiche loc link</td>
    <td><? echo $_SESSION['cpt']['id_fiche_loc_link']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['id_fiche_loc_link'],$_SESSION['cpt']['valide_comment']['id_fiche_loc_link']); ?></td>
  </tr>
  <tr>
    <td>copmte d&eacute;bit&eacute;</td>
    <td><? echo $_SESSION['cpt']['no_compte_debit']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['no_compte_debit'],$_SESSION['cpt']['valide_comment']['no_compte_debit']); ?></td>
    <td>--libell&eacute;--&gt;</td>
    <td><? echo $_SESSION['cpt']['libelle_compte_debit']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['verrou_compte_debit'],$_SESSION['cpt']['valide_comment']['verrou_compte_debit']); ?></td>
  </tr>
  <tr>
    <td>compte cr&eacute;dit&eacute;</td>
    <td><? echo $_SESSION['cpt']['no_compte_credit']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['no_compte_credit'],$_SESSION['cpt']['valide_comment']['no_compte_credit']); ?></td>
    <td>--libell&eacute;--&gt;</td>
    <td><? echo $_SESSION['cpt']['libelle_compte_credit']; ?></td>
    <td><? echo affiche_petit_point($_SESSION['cpt']['valide']['verrou_compte_credit'],$_SESSION['cpt']['valide_comment']['verrou_compte_credit']); ?></td>
  </tr>
</table>
<?
if (in_array(0,$_SESSION['cpt']['valide']))
	{
	print "au moins 1 erreur";
	$finaliser=0;
	}
elseif (in_array(0.5,$_SESSION['cpt']['valide']))
	{
	print "manque au moins 1 donnée non indispensable";
	$finaliser=1;
	}
else
	{
	print "tous les champs valides!";
	$finaliser=1;
	}
	;
if ($finaliser==1 && $_GET['step']!=99)
	{
	$_SESSION['rand_secu']=rand(1000000,1000000000000);
	echo '<br><a href="?step=99&rand_secu='.$_SESSION['rand_secu'].'">CONFIRMER !!</a>';
	
	}
?>
</body>
</html>
		